package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    //页面数据和分页
    @RequestMapping("/list")
    public String List(Model model, @ModelAttribute("qo") TravelQuery qo) {
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page", page);
        return "travel/list";
    }


    //删除
    @GetMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        travelService.delete(id);
        return JsonResult.success();
    }


    //添加或修改
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        travelService.saveOrUpdate(travel,request);
        return JsonResult.success();
    }

    //审核操作
    @GetMapping("/audit")
    @ResponseBody
    public Object audit(String id, int state) {
        travelService.audit(id, state);
        return JsonResult.success();
    }

    //查看功能
    @GetMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id) {
        String content = travelService.get(id).getContent();
        return JsonResult.success(content);
    }
}
